<?php

namespace app\common\service;

use app\common\service\Base;
use app\common\service\OrderMaterial as OrderMaterialService;

class MaterialReturn extends Base
{

	/**
	 * 根据ID获取记录信息
	 *
	 * @author KING
	 * @date 2020年7月9日
	 *
	 * @param integer $id
	 * @param string $field
	 * @return array
	 */
	public function getInfoById($id, $field='*') {
		if (empty($id)) return [];
		return db('material_return')->where('id', $id)->field($field)->findOrEmpty();
	}

	/**
	 * 记录操作日志
	 *
	 * @author King
	 * @date 2020年9月28日
	 *
	 * @param integer $materialReturnId		退换料记录ID
	 * @param integer $type					类型（1发起退料，2操作入库，3确认退料，4确认换料，5确认审核）
	 * @param string $remark
	 */
	public function addLog($materialReturnId, $type, $remark='') {
        if ($type == 1) {
            (new OrderMaterialService())->material_return_update(0, $materialReturnId);
        }
		$typeArr = ['1' => '发起退料', '2' => '操作入库', '3' => '确认退料', '4' => '确认换料', '5' => '确认审核'];
		if ($remark == '') {
			$remark = arrayGet($typeArr, $type, '');
		}
		$data = [
			'material_return_id' => $materialReturnId,
			'type' => $type,
			'remark' => $remark,
			'admin_id' => ADMIN_ID,
			'create_time' => time(),
		];
		return db('material_return_log')->insertGetId($data);
	}
}